<template>
  <div>
    <br />
    <br />
    <router-link
      to="/systemManage/user"
      style="margin-left: 40px;margin-top: 20px;color:blue;"
    >返回用户管理</router-link>
    <h2 style="text-align: center;">用户角色编辑</h2>
    <el-row :gutter="20" style="margin-left: 40px;">
      <el-col :span="5">当前编辑账号：{{editInfo.username}}</el-col>
      <el-col :span="5">当前编辑用户名：{{editInfo.name}}</el-col>
      <el-col :span="5">归属机构：{{editInfo.comName}}</el-col>
      <el-col :span="5">所属部门：{{editInfo.department}}</el-col>
    </el-row>
    <el-table :data="tableData" style="width: 75%;margin: 30px 40px;">
      <el-table-column prop="id" label="ID" width="45"></el-table-column>
      <el-table-column prop="roleName" label="角色名" width="200"></el-table-column>
      <el-table-column prop="roleDesc" label="角色描述" width="190"></el-table-column>
      <el-table-column prop="status" label="角色权限" width="80"></el-table-column>
      <el-table-column prop="updateTime" label="最后修改时间" width="150"></el-table-column>
      <el-table-column fixed="right" label="操作" width="125">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClick(scope.row)" size="small">添加或移除权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="save" style="margin: 20px 40%;">保存提交</el-button>
  </div>
</template>

<script>
import { getRoles, updateUserRole } from "@/api/user";

export default {
  name: "editRole",
  data() {
    return {
      editInfo: this.$route.query.editInfo,
      tableData: []
    };
  },
  mounted() {
    getRoles(this.editInfo.systemCode, this.editInfo.id).then(res => {
      for (let i = 0; i < res.data.length; i++) {
        if (res.data[i].status === 1) res.data[i].status = "有";
        else res.data[i].status = "无";
      }
      this.tableData = res.data;
    });
  },
  methods: {
    save() {
      const data = this.tableData;
      let roleId = [];
      let status = [];
      for (let i = 0; i < data.length; i++) {
        roleId.push(data[i].id);
        if (data[i].status === "有") {
          status.push(1);
        } else {
          status.push(0);
        }
      }
      updateUserRole({
        systemCode: this.editInfo.systemCode,
        userId: this.editInfo.id,
        roleIds: roleId.toString(),
        status: status.toString()
      }).then(res => {
            this.$message({
              message: res.message,
              type: "success"
            });
      });
    },
    handleClick(row) {
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].id === row.id) {
          if (this.tableData[i].status === "有") {
            this.tableData[i].status = "无";
            this.$message({
              message: "已移除 " + row.roleName + " 角色权限",
              type: "warning"
            });
          } else {
            this.tableData[i].status = "有";
            this.$message({
              message: "已添加 " + row.roleName + " 角色权限",
              type: "success"
            });
          }
          break;
        }
      }
    }
  }
};
</script>

<style scoped>
</style>
